package com.yanqu.road.server.cmd.newmonopolymkt.res;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_NEW_MONOPOLY_MARKET_CHANGE_TEAM_CONFIRM_FROM_CROSS, desc = "修改队伍")
public class NewMonopolyMarketResGrabChangeTeamConfirmCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketCrossResGrabChangeTeamConfirmRespMsg msg = NewMonopolyMarketProto.NewMonopolyMarketCrossResGrabChangeTeamConfirmRespMsg.parseFrom(packet.getBytes());
        if(msg.getAction() == 1){
            if(msg.getStcMsg().getRet() == 0){
                NewMonopolyMarketProto.NewMonopolyMarketSynPointSettlementToGameServerMsg settlement = msg.getStcMsg().getSettlement();
                Property reward = NewMonopolyMarketMgr.addReward(settlement.getType(), settlement.getRewardNum());
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.NewMonopolyMarket, eLogMoneyType.NewMonopolyMarketResGrabReplaceRecallReward);

                NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.Builder builder  = NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.newBuilder();
                builder.setType(2);
                builder.addResGrabTip(NewMonopolyMarketMgr.builderMonopolyMarketResGrabTipEntity(settlement));
                player.sendPacket(YanQuMessageUtils.buildMessage(ClientProtocol.U_NEW_MONOPOLY_MARKET_BROADCAST, builder));

                NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
                marketModule.userData.setWaitDispatchPoint(null);

                //同一类资源
                marketModule.addDispatchInfo(msg.getStcMsg().getResGroupId(), settlement.getType(), msg.getStcMsg().getPointInfo().getPosIndex(),
                        msg.getStcMsg().getDispatchObjectIdList(), msg.getStcMsg().getPointInfo().getStartTime());

                log.info("榷场玩家{}， 占领资源组{}-位置{}，类型{}， 队伍{}", player.getUserId(), msg.getStcMsg().getResGroupId(), msg.getStcMsg().getPointInfo().getPosIndex(), settlement.getType(), StringUtils.listToString(msg.getStcMsg().getDispatchObjectIdList(), ","));

                NewMonopolyMarketMgr.leaveResGrabPoint(player, settlement.getResGroupId(), settlement.getPosIndex(),true);
                log.info("榷场玩家{}， 撤回资源组{}-位置{}，类型{}， 队伍{}", player.getUserId(), settlement.getResGroupId(), settlement.getPosIndex(), settlement.getType(), StringUtils.listToString(settlement.getDispatchObjectIdList(), ","));

            }
        }
        player.sendPacket(Protocol.U_NEW_MONOPOLY_MARKET_CHANGE_TEAM_CONFIRM, msg.getStcMsg().toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
